<?php

require_once 'OAuth/Utils.php';

/**
 * This is class Model_DbTable_Registries
 *
 */
class Model_DbTable_Registries extends Zend_Db_Table {	
	/**
	 * Table name
	 *
	 * @var mixed 
	 *
	 */	
	protected $_name = 'registries';
	
	/**
	 * Primary key
	 *
	 * @var mixed 
	 *
	 */	
	protected $_primary = 'id';
	
	/**
	 * This is method getRegistry
	 *
	 * @param mixed $id This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function getRegistry($id) {
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Count not find row $id");
		}
		return $row->toArray();    
	}
	
	/**
	 * This is method getRegistryByKey
	 *
	 * @param mixed $key This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function getRegistryByKey($key, $enabled = 1) {
		$where = array($this->getAdapter()->quoteInto('api_key = ?', $key), 
			$this->getAdapter()->quoteInto('enabled = ?', $enabled)); 
		
		$row = $this->fetchRow($where);
		if (!$row) {
			throw new Exception('No consumer with api_key "' . $key .
				'" and enabled "' . $enabled . '"');
		}
		return $row->toArray(); 
	}	
	
	/**
	 * Get consumer application
	 *
	 * @param string $apiKey The API key
	 * @param int $userId The User ID
	 * @param boolean $isAdmin Is consumer application or not
	 * @return array The consumer application information
	 *
	 */
	public function getConsumerByUser($key, $userId, $isAdmin = false) {		
		$row = $this->getConsumer($key, 1);
		
		if (!$isAdmin && !empty($row['user_id']) && $row['user_id'] != $userId) {
			throw new Exception('No access to the consumer information for api_key "' . 
				$key . '"');
		}
		
		return $row;
	}		
	
	/**
	 * This is method addRegistry
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function addRegistry($userId, $enabled, $status, 
		$requesterName, $requesterEmail, $callbackUri, $appUri,
		$appTitle, $appDesc, $appNotes, $appLogo, $appType, $appCommercial)
	{				
		$key = Utils::generateKey(true);
		$secret = Utils::generateKey();
		$issuedDate = date('Y-m-d H:i:s');
		
		$data = array(
			'user_id' => $userId,
			'consumer_key' => $key,
			'consumer_secret' => $secret,
			'enabled' => $enabled,
			'status' => $status,
			'requester_name' => $requesterName,
			'requester_email' => $requesterEmail,
			'callback_uri' => $callbackUri,
			'app_uri' => $appUri,
			'app_title' => $appTitle,
			'app_desc' => $appDesc,
			'app_notes' => $appNotes,
			'app_logo' => $appLogo,
			'app_type' => $appType,
			'app_commercial' => $appCommercial,		
			'issued_date' => $issuedDate
			);
		
		$this->insert($data);
	}
	
	/**
	 * This is method updateRegistry
	 *
	 */	
	function updateRegistry($id, $userId, $enabled, $status, 
		$requesterName, $requesterEmail, $callbackUri, $appUri, $appTitle, 
		$appDesc, $appNotes, $appLogo, $appType, $appCommercial, $issuedDate = '')
	{
		if(!$issuedDate || empty($issuedDate)) {
			$issuedDate = date('Y-m-d H:i:s');
		}		
		
		$data = array(
			'user_id' => $userId,
			'enabled' => $enabled,
			'status' => $status,
			'requester_name' => $requesterName,
			'requester_email' => $requesterEmail,
			'callback_uri' => $callbackUri,
			'app_uri' => $appUri,
			'app_title' => $appTitle,
			'app_desc' => $appDesc,
			'app_notes' => $appNotes,
			'app_logo' => $appLogo,
			'app_type' => $appType,
			'app_commercial' => $appCommercial,		
			'issued_date' => $issuedDate
			);	
		
		$this->update($data, 'id = '. (int)$id);
	}
	
	/**
	 * This is method deleteRegistry
	 *
	 * @param mixed $id This is a description
	 * @return mixed This is the return value description
	 *
	 */
	function deleteRegistry($id) {
		$this->delete('id =' . (int)$id);
	}
	
	/**
	 * This is method deleteRegistryByKey
	 *
	 * @param mixed $key This is a description
	 * @return mixed This is the return value description
	 *
	 */
	function deleteRegistryByKey($key) {
		$this->delete('consumer_key =' . $key);
	}	
}
